package
{
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Elastic;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	public class Store implements ISelectable, IPersistable, ITooltipable
	{
		public var id:int;
		public var value:int;
		public var name:String;
		public var owner:Player;
		public var display:Sprite;
		public var width:int;
		private var m_location:Point = new Point();
		private var storeNameTF:TextField = new TextField();
		private var m_currentStock:int;
		private var m_maxStock:int;
		private var m_stockTF:TextField = new TextField();
		private var m_menu:Sprite;
		public var quality:int = 1;
		
		private var m_xp:int = 0;
		public var level:int = 1;
		public var statPoints:int = 0;
		
		private var m_oosDisplay:Sprite;
		
		private var m_levelUpDisplay:Sprite;
		public var mall:Mall;
		
		public function Store()
		{				
		}
		
		
		public function getTooltip():String{
			var txt:String = '';
			txt += Util.uCaseFirst(name) + ' Shop' + "\n";
			txt += 'Level: ' + this.level + "\n";
			txt += 'XP: ' + m_xp + '/' + getXPForNextLevel() + "\n";
			txt += 'Stock: ' + m_currentStock + '/' + m_maxStock + "\n";
			txt += 'Quality: ' + quality;
			
			return txt;
		}
		
		public function getShopName():String{
			return Util.uCaseFirst(name) + ' Shop';
		}
		
		public function getXPForNextLevel():int{
			var initLevelCurve:int = 5;
			var var1:Number = Math.pow((level + initLevelCurve)*3/initLevelCurve, 2);
			return Math.floor(var1);
		}
		
		public function updateXP(delta:int):void{
			m_xp += delta;
			if(m_xp >= getXPForNextLevel()){
				level++;
				m_xp = 0;
				statPoints += 2;
				showLevelUp();
			}
		}
		
		public function getMenu():Sprite{
			updateMenu();
			return m_menu;
		}
		
		
		
		public function updateMenu():void{
			Util.removeChildren(m_menu);
			var move:Sprite = Util.getButton("Move",0,.7);
			move.addEventListener(MouseEvent.CLICK, onMove);
			m_menu.addChild(move);
			Util.buttonize(move);
			
			if(statPoints > 0){
				var stats:Sprite = Util.getButton("Assign Points",0,.7);
				stats.y = m_menu.height + Game.PADDING;
				m_menu.addChild(stats);
				Util.buttonize(stats);	
				stats.addEventListener(MouseEvent.CLICK, onAssignPoints);
			}
		}
		
		private function onMove(me:MouseEvent):void{
			var gm:GMStorePlacement = new GMStorePlacement(this.name, GMStorePlacement.MODE_MOVE);
			gm.storeReference = this;
			Game.instance.enterGameMode(gm);
		}
		
		public function onAssignPoints(me:MouseEvent):void{
			var statAssigner:StatAssigner = new StatAssigner(this);
			statAssigner.onCloseCallback = function():void{
				Game.instance.hud.hideMainDialog();
			}
			Game.instance.hud.showAsMainDialog(statAssigner.display);
			
		}
		
		public function changeStock(delta:int):void{
			if(m_currentStock <= 0 && (m_currentStock + delta) > 0){
				hideOutOfStock();				
			}
			currentStock += delta;
			Util.bubbleTextOut(m_stockTF, Game.instance.mall.display, String(delta), 0xff0000, 12);
			if(m_currentStock <= 0){
				showOutOfStock();
			}			
		}
		
		public function getRectangle():Rectangle{
			return new Rectangle(location.x, location.y, width, 1);
		}		
		
		public function init(name:String):void{
			var config:Object = Game.config.getStore(name);
			this.name = config['name'];	
			value = config['value'];
			width = Game.config.baseStoreWidth;
			m_currentStock = 0;
			m_maxStock = Game.config.baseStock;
			display = Util.getRectSprite(width*Game.PPS,1*Game.PPS,config['color']);
			display.addChild(storeNameTF);
			Util.applyStyleH2(storeNameTF);

			storeNameTF.text = Util.uCaseFirst(name);
			storeNameTF.autoSize = TextFieldAutoSize.LEFT;
			storeNameTF.x = display.width/2 - storeNameTF.width/2;
			storeNameTF.y = display.height/2 - storeNameTF.height/2;

			
			m_oosDisplay = Util.getRoundRectSprite(display.width, display.height, 0x999999);
			var oosTF:TextField = new TextField();
			m_oosDisplay.addChild(oosTF);
			oosTF.width = display.width;
			oosTF.height = display.height;
			Util.applyStyleH2(oosTF, 0xffffff);
			oosTF.htmlText = '<p align="center">Sold\nOut</p>';
			oosTF.wordWrap = true;
			//m_oosDisplay.visible = false;
			m_oosDisplay.alpha = 0;
			display.addChild(m_oosDisplay);
			
			
			m_levelUpDisplay = Util.getRoundRectSprite(display.width, display.height, 0, .8);
			var levelupTF:TextField = new TextField();
			m_levelUpDisplay.addChild(levelupTF);
			levelupTF.width = display.width;
			levelupTF.height = display.height;
			Util.applyStyleH2(levelupTF, 0x0099ff);
			levelupTF.htmlText = '<p align="center">Level\nUp!</p>';
			levelupTF.wordWrap = true;
			m_levelUpDisplay.alpha = 0;
			//m_levelUpDisplay.visible = false;			
			display.addChild(m_levelUpDisplay);
			
			m_menu = new Sprite();
			m_menu.addEventListener(MouseEvent.CLICK, function():void{
				mall.closeAllMenus();
			});
			
			
			Util.applyStyleH4(m_stockTF, 0xffff00);
			m_stockTF.x = Game.PADDING/5;
			m_stockTF.y = Game.PADDING/5;
			m_stockTF.autoSize = TextFieldAutoSize.LEFT;
			m_stockTF.visible = false;
			display.addChild(m_stockTF);
			
			
		}
		
		public function showStockInfo():void{
			m_stockTF.visible = true;
		}
		
		public function get maxStock():int{
			return m_maxStock;
		}
		
		public function set maxStock(val:int):void{
			m_maxStock = val;
		}
		
		public function get currentStock():int{
			return m_currentStock;
		}
		
		public function set currentStock(value:int):void{
			m_currentStock = value;
			m_stockTF.text = String(m_currentStock) + "/" + String(m_maxStock);
			m_stockTF.autoSize = TextFieldAutoSize.LEFT;
		}
		
		public function showOutOfStock():void{
			m_oosDisplay.visible = true;
			Util.scale(m_oosDisplay, 0, .95, .5, .5,1);
		}
		
		public function hideOutOfStock():void{
			if(m_oosDisplay.alpha == 1){
				Util.scale(m_oosDisplay, .95, 0, 1, 1, 0);
			}
		}
		
		public function showLevelUp():void{
			if(m_levelUpDisplay.alpha == 0){
				Util.scale(m_levelUpDisplay, 0, .95, .5, .5,1);
			}
		}
		
		public function hideLevelUp():void{
			if(m_levelUpDisplay.alpha == 1){
				Util.scale(m_levelUpDisplay, .95, 0, 1, 1, 0);
			}
		}		
		
		public function set location(location:Point):void{
			display.x = location.x * Game.PPS;
			display.y = -location.y * Game.PPS - display.height;
			m_location.x = location.x;
			m_location.y = location.y;
			
		}
		
		public function get location():Point{
			return m_location;
		}
		
		public function hitTestPoint(point:Point):Boolean{
			return display.hitTestPoint(point.x, point.y);
		}
		public function highlite(val:Boolean):void{
			if(val){
				display.filters = [new GlowFilter(0x00ffff)];
			} else {
				display.filters = [];
			}
		}
		
		public function getSaveObject():Object{
			
			var saveObj:Object = new Object();
			saveObj['id'] = id;
			saveObj['name'] = name;
			saveObj['width'] = width;
			saveObj['location'] = new Object();
			saveObj['location']['x'] = m_location.x;
			saveObj['location']['y'] = m_location.y;
			
			return saveObj;
		}
		
		public function loadSavedObject(savedObj:Object):void{
			init(savedObj['name']);
			
			location = new Point(savedObj['location']['x'], savedObj['location']['y']);
		}
	}
}